$(function () {

    $("#entryTime").datetimepicker({//选择年月日
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,//显示‘今日’按钮
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,  //Number, String. 默认值：0, 'hour'，日期时间选择器所能够提供的最精确的时间选择视图。
        clearBtn:true,//清除按钮
        forceParse: 0
    });
    $("#leaveTime").datetimepicker({//选择年月日
        format: 'yyyy-mm-dd',
        language: 'zh-CN',
        weekStart: 1,
        todayBtn: 1,//显示‘今日’按钮
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,  //Number, String. 默认值：0, 'hour'，日期时间选择器所能够提供的最精确的时间选择视图。
        clearBtn:true,//清除按钮
        forceParse: 0
    });
    $("#jqGrid").jqGrid({
        url: baseURL + 'manage/practitioners/list',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '姓名', name: 'name', index: 'name', width: 80 },
            { label: '公司名称', name: 'corporateName', index: 'corporateName', width: 80 },
            { label: '职务', name: 'job', index: 'job', width: 80 },
            { label: '身份证号码', name: 'idCard', index: 'id_card', width: 80 },
            { label: '联系电话', name: 'phone', index: 'phone', width: 80 },
            { label: '家庭住址', name: 'addr', index: 'addr', width: 80 },
            { label: '入职时间', name: 'entryTime', index: 'entry_time', width: 80,
                formatter:"date",formatoptions: {srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'}
            },
            { label: '辞（离）职', name: 'leaveTime', index: 'leave_time', width: 80,
                formatter:"date",formatoptions: {srcformat:'Y-m-d H:i:s',newformat:'Y-m-d'}
            },
            { label: '监管事由', name: 'cause', index: 'cause', width: 80 },
            { label: '监管状态', name: 'effState', index: 'eff_state', width: 80 ,formatter:runStatus},
            { label: '监管结果', name: 'result', index: 'result', width: 80 },
            { label: '开除原因', name: 'reason', index: 'reason', width: 80 },
            { label: '备注', name: 'remarks', index: 'remarks', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

    function runStatus(cellValue,optios,rowObject){
        var text = "有效";
        switch (rowObject.effState) {
            case 1:
                text = "有效";
                break;
            case 2:
                text = "无效";
                break;
            case 3:
                text = "撤销";
                break;
            default:
                text = "其他";
        }
        return text;
    }
});
var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            name: null
        },
        showList: true,
        title: null,
        practitioners: {}
    },
    methods: {
        query: function () {
            console.log(vm.q)
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.practitioners = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        cancel:function(event){
            var id = getSelectedRow();
            if(event === null){
                alert("请选择一条记录");
                return ;
            }
            layer.confirm('确定要撤销选中的记录？', {
            }, function(){
                    $.ajax({
                        type: "POST",
                        url: baseURL + "manage/practitioners/cancel",
                        dataType: "json",
                        data: {id:id},
                        success: function(r){
                            if(r.code === 0){
                                layer.msg("操作成功", {icon: 1});
                                vm.reload();
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
            }, function(){
            });
        },
/*        upload: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "上传";

            vm.getInfo(id)
        },*/
        saveOrUpdate: function (event) {
            vm.practitioners.entryTime = $("#entryTime").data("datetimepicker").getDate();
            vm.practitioners.leaveTime = $("#leaveTime").data("datetimepicker").getDate();
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
            var url = vm.practitioners.id == null ? "manage/practitioners/save" : "manage/practitioners/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.practitioners),
                success: function(r){
                    if(r.code === 0){
                        layer.msg("操作成功", {icon: 1});
                        vm.reload();
                        $('#btnSaveOrUpdate').button('reset');
                        $('#btnSaveOrUpdate').dequeue();
                    }else{
                        layer.alert(r.msg);
                        $('#btnSaveOrUpdate').button('reset');
                        $('#btnSaveOrUpdate').dequeue();
                    }
                }
            });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "manage/practitioners/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function(id){
            $.get(baseURL + "manage/practitioners/info/"+id, function(r){
                vm.practitioners = r.practitioners;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
        }
    }
});
//导入excel文件
$("#saveZipButton").on('click', function(){
    var formData = new FormData();
    var name = $("#articleImageFile").val();
    formData.append("file",$("#articleImageFile")[0].files[0]);
    formData.append("name",name);//这个地方可以传递多个参数
    $.ajax({
        url :  baseURL + "manage/practitioners/batchImport",
        type : 'POST',
        async : false,
        data : formData,
        // 告诉jQuery不要去处理发送的数据
        processData : false,
        // 告诉jQuery不要去设置Content-Type请求头
        contentType : false,
        beforeSend:function(){
            console.log("正在进行，请稍候");
        },
        success : function(responseStr) {
            if(responseStr=="01"){
                alert("导入成功");
            }else{
                alert("导入失败");
            }
        }
    });
});